<?php
class Coach_model
    extends CI_Model
    {
    
    function __construct()
        {
        parent::__construct();
        }
    
    function get_all()
    {
        $query=$this->db->query("SELECT u.userID, c.name, u.loginName, u.class_id
        FROM users as u
        LEFT JOIN class as c ON u.class_id=c.id
        WHERE u.class_id IN (1,4,5) 
        ORDER BY u.class_id DESC, u.loginName ASC");
        return $query;
    }
    
    function parseScoutList($query)
    {
        
        $this->load->library('table');
      
        
        foreach ($query->field_data() as $head)
        {
           $headers[]=$head->name; 
        }
        $headers[]= 'result';
        $this->table->set_heading($headers);
        foreach ($query->result_array() as $row)
        {
            if ($row['class_id']!=1) $row['result']=$this->_renderButtons($row);
            else $row['result']='';
            $this->table->add_row($this->_prep_for_table($row));
            
        }
        return $this->table->generate();
    }
    
    //функция перевода массива из ассоциативного в числовой
    function _prep_for_table($row)
    {
        $out=array();
        foreach ($row as $value)    
        {
            $out[]=$value;
        }
        return $out;
    }
    
    function update_scout($userID='', $action)
    {
        $userID = (int) $userID;
        if (!$userID) return false;
        
        switch ($action)
        {
            case 'del': $class_id=3; break;
            case 'add_coach':
            case 'coach': $class_id=5; break;
            case 'scout':
            case 'add_scout': $class_id=4; break;
        }
        $query=$this->db->get_where('users', array('userID'=>$userID));
        if ($query->num_rows()>0)
        {
            $user_row=$query->result_array();
            if ($user_row[0]['class_id']!=1)  //если это не админ!
            return $this->db->query('UPDATE users SET class_id='.$this->db->escape($class_id).' WHERE userID='.$this->db->escape($userID));
        }
        else return false;
    }

    function _convert()
    {
        $query=$this->db->query("SELECT * FROM usersRoles");
        foreach ($query->result_array() as $role)
        {
            if ($role['adminRole']) $class_id=1;
            elseif ($role['coachRole']) $class_id=5;
            elseif ($role['scoutRole']) $class_id=4;
            else $class_id=0;
            
            $this->db->query("UPDATE users SET class_id=$class_id WHERE userID=".$role['userID']);
        }
    }
    
    function _renderButtons($data)
    {
        
        $out='';
        $out.='<a href="'.site_url().'coach/scouts/del/'.$data['userID'].'">Del</a> | ';
        $out.='<a href="'.site_url().'coach/scouts/scout/'.$data['userID'].'">Scout</a> | ';
        $out.='<a href="'.site_url().'coach/scouts/coach/'.$data['userID'].'">Coach</a>';
        return $out;
    }
    }
?>